// LEC    March 26, 2021
//
// STATE SIZE PAPER
// Analysis file system level with Bridging Data


// PREPARE DATA FOR TABLE 5
cd $OUTDIR

use ethbc, clear

tab birtha deathb

tab birtha deathb if ethchange==1


cd $OUTDIR

use analysis_data, clear

drop if year<$STARTYEAR


bys year: egen sumarea = sum(area)

bys year: egen sysconc = sum((area/sumarea)^2)


// Weighted means

bys year: egen efaw = sum(area*ef)
replace efaw = efaw/sumarea

bys year: egen agfracaw = sum(area*agfrac)
replace agfracaw = agfracaw/sumarea


bys year: egen maxshareaw = sum(area*maxshare)
replace maxshareaw = maxshareaw/sumarea

bys year: egen maxagshareaw = sum(area*maxagshare)
replace maxagshareaw = maxagshareaw/sumarea


bys year: egen intdjaw = sum(area*intdj)
replace intdjaw = intdjaw/sumarea

bys year: egen extdjaw = sum(area*extdj)
replace extdjaw = extdjaw/sumarea




bys year: egen sumarea1 = sum(area*oldstate)
bys year: egen sumarea2 = sum(area*(1-oldstate))

bys year: egen efaw1 = sum(area*ef*oldstate)
replace efaw1 = efaw1/sumarea1

bys year: egen agfracaw1 = sum(area*agfrac*oldstate)
replace agfracaw1 = agfracaw1/sumarea1

bys year: egen efaw2 = sum(area*ef*(1-oldstate))
replace efaw2 = efaw2/sumarea2

bys year: egen agfracaw2 = sum(area*agfrac*(1-oldstate))
replace agfracaw2 = agfracaw2/sumarea2



bys year: egen sumpop0=sum(population)

bys year: egen efpw = sum(population*ef)
replace efpw = efpw/sumpop0

bys year: egen agfracpw = sum(population*agfrac)
replace agfracpw = agfracpw/sumpop0

bys year: egen sumpop1 = sum(population*oldstate)
bys year: egen sumpop2 = sum(population*(1-oldstate))

bys year: egen efpw1 = sum(population*ef*oldstate)
replace efpw1 = efpw1/sumpop1

bys year: egen agfracpw1 = sum(population*agfrac*oldstate)
replace agfracpw1 = agfracpw1/sumpop1

bys year: egen efpw2 = sum(population*ef*(1-oldstate))
replace efpw2 = efpw2/sumpop2

bys year: egen agfracpw2 = sum(population*agfrac*(1-oldstate))
replace agfracpw2 = agfracpw2/sumpop2

bys year: egen syspopconc = sum((population/sumpop0)^2)


sort idc year
xtset idc year

bys year: egen medarea = median(area)


//////////// Collapse data to system level ///////////////////////////////////////////////////


collapse (max)n /*n1 n2*/ (mean)sumarea area medarea /*area1 area2*/ lnarea maxshare /*maxshare1 maxshare2*/ ///
(mean)ef /*ef1 ef2*/ agfrac /*agfrac1 agfrac2*/ efaw /*efaw1 efaw2*/ agfracaw /*agfracaw1 agfracaw2*/ efpw agfracpw /*efpw1 agfracpw1 efpw2 agfracpw2*/ maxagshare maxshareaw maxagshareaw ///
intdj intdjaw extdj extdjaw ///
(mean)sysconc syspopconc /*(mean)wrsysconc*/ (sum)terrnull transfera absorba absorbb secessiona secessionb collapsea collapseb ethseca ethsecb irra ethabsorbb ///
(sum) ethcollapsea ethcollapseb, by (year) ///
// cumularea lncumularea cumuletharea lncumuletharea cumulnetharea lncumulnetharea


drop if year<$STARTYEAR


tset year

gen bc = transfera + absorbb + secessiona + collapsea
gen ethbc = irra + ethabsorbb + ethseca + ethcollapseb

gen neabsorb = absorbb - ethabsorbb
gen nesec = secessiona - ethseca
gen necollapsea = collapsea - ethcollapsea
gen necollapseb = collapseb - ethcollapseb

// net border change
gen dn = secessiona + collapsea - absorbb -collapseb + terrnull
gen ethdn = ethseca + ethcollapsea - ethabsorbb - ethcollapseb +terrnull
gen nethdn = nesec + necollapsea - neabsorb - necollapseb + terrnull

gen one = 1
bys one (year): gen cumuldn = sum(dn)
bys one (year): gen cumulethdn = sum(ethdn)
bys one (year): gen cumulnethdn = sum(nethdn)

gen startyear = 0
replace startyear = 1 if year == $STARTYEAR

bys one: egen n0 = max(startyear*n)
bys one: egen area0 = max(startyear*sumarea)

gen reln = n0 + cumuldn
gen delta = reln - n

gen totarea = sumarea/(n0 + cumuldn)
gen etharea = sumarea/(n0 + cumulethdn)
gen netharea = sumarea/(n0 + cumulnethdn)



gen neth = n0 + cumulethdn
gen nneth = n0 + cumulnethdn

cd $OUTDIR
save system.dta, replace

/////////////////////////////////////////////////////////////////////////
// CREATE STATE SIZE COMPARISON WITH LAKE, CSHAPES-EUROPE AND CSHAPES 2.0 DATA (SEE FIGURE 1 IN PAPER)

cd $COMPARISONDIR

merge 1:1 year using cshp_system
drop _merge


merge 1:1 year using statesize_lake
drop _merge


// OUTPUT FIGURE 1 
tsline area cshp_area terrkm, xla(1800(50)2000) yla(0(500000) 2500000, angle(0)) graphregion(color(white)) scheme(s2mono) ///
legend(order(1 "Europe (CShapes-Europe)" 2 "World (CShapes 2.0)" 3 "World (Lake & O'Mahony)")) ytitle(Mean State Size)
graph export StateSize3Trends.png, width(1275) replace
graph export StateSize3Trends.pdf, replace

cd $OUTDIR
merge 1:1 year using agsystemvars
drop _merge



////////////////////////////////////////////////////////////////////////

cd $OUTDIR
clear
use system.dta
 
global G xla(1880(20)2020)  graphregion(color(white)) bgcolor(white)
global G xla(1880(10)2020)  graphregion(color(white)) bgcolor(white)
global G2 xla(1850(50)2020)  graphregion(color(white)) bgcolor(white)
global R "(1800(50)2020)"
global R1 "(1880(10)2020)"   

 cd $OUTPUTDIR

 /* NOT USED
tsline area, yla(0(500000)2500000) xla(1880(20)2020) ylabel(, angle(0)) ytitle(Mean State Size) ///
 graphregion(color(white)) bgcolor(white) scheme(s2mono)
graph export StateSize_Europe.png, width(1275) replace
graph export StateSize_Europe.pdf, replace
*/

// OUTPUT FIGURE A2: TREND IN NUMBER OF STATES
tsline n, xla(1800(50)2020) yla(0(10)60) ytitle(Number of States) graphregion(color(white)) bgcolor(white) scheme(s2mono)
graph export NStates_Europe.png, width(1275) replace
graph export NStates_Europe.pdf, replace

// OUTPUT FIGURE 5: FRACTIONALIZATION TRENDS
twoway (bar area year, yaxis(1) color(gs13) ylabel(, angle(0))) (line efaw year, yaxis(2)  lwidth(medium) lpattern(solid)) (line agfracaw year, yaxis(2) lwidth(medium) lpattern(dash)), /// 
graphregion(color(white)) bgcolor(white) scheme(s2mono) xla(1800(50)2020) ///
legend (order(1 "State Size" 2  "Ethnic Frac. Country" 3 "Terr. Frac. Kin Group")) ///
ytitle(Mean State Size)
graph export FracGraph_Europe.png, width(1275) replace
graph export FracGraph_Europe.pdf, replace


/* NOT USED
twoway (bar area year, yaxis(1) color(gs13) ylabel(, angle(0))) (line ef year, yaxis(2)  lwidth(medium) lpattern(solid)) (line agfrac year, yaxis(2) lwidth(medium) lpattern(dash)), /// 
graphregion(color(white)) bgcolor(white) scheme(s2mono) xla(1800(50)2020) ///
legend (order(1 "State Size" 2  "Ethnic Frac. Country" 3 "Terr. Frac. Kin Group")) ///
ytitle(Mean State Size)
*/

// OUTPUT FIGURE 9: COMPARING ETHNIC AND NON-ETHNIC TRENDS 
// OUTPUT FIGURE A4: SAME BUT RUN WITH FIXED HEG DATA
tsline totarea etharea netharea, yla(0(100000)500000) xla(1800(50)2020) ylabel(, angle(0)) ytitle(Mean state size)  ///
legend(order(1 "Actual state size" 2 "Ethnic trend" 3 "Non-ethnic trend")) graphregion(color(white)) bgcolor(white) scheme(s2mono) 
 graph export Trends_Europe.png, width(1275) replace
graph export Trends_Europe.pdf, replace

/* NOT USED 
twoway (line efaw year, lwidth(thick) lpattern(solid) lcolor(red)) (line agtfaw year, lwidth(thick) lpattern(dash) lcolor(green)), /// 
graphregion(color(white)) bgcolor(white) scheme(s2mono) xla(1800(50)2020) yla(0(0.1)0.7) ///
legend (order(1  "Ethnic fractionalization" 2 "Territorial fractionalization")) ///
ytitle()
// graph export FracGraph_Europe.png, width(1275) replace
//graph export FracGraph_Europe.pdf, replace


twoway (line efpw year, lwidth(thick) lpattern(solid) lcolor(red)) (line agtfpw year, lwidth(thick) lpattern(dash) lcolor(green)), /// 
graphregion(color(white)) bgcolor(white) scheme(s2mono) xla(1800(50)2020) yla(0(0.1)0.7) ///
legend (order(1  "Ethnic fractionalization" 2 "Territorial fractionalization")) ///
ytitle()

twoway (bar area year, yaxis(1) color(gs13) ylabel(, angle(0))) (line efaw year, yaxis(2)  lwidth(thick) lcolor(red) lpattern(solid)) (line agfracaw year, yaxis(2)  lwidth(thick) lcolor(green) lpattern(dash)), /// 
graphregion(color(white)) bgcolor(white) scheme(s2mono) xla(1800(50)2020)  ///
legend (order(1 "State Size" 2  "Ethnic Frac. Country" 3 "Terr. Frac. Kin Group")) ///
ytitle(Mean State Size)
 */
 

////////////////////////////////////////////////////////////////////////
// Create bar charts
////////////////////////////////////////////////////////////////////////

cd $OUTDIR
clear
use system.dta
   
gen year5 = int((year+2)/5)
//replace year = year5

collapse (sum)bc secessiona collapsea transfera absorbb ethseca irra ethabsorbb ethcollapsea, by (year5)


gen secession = .
gen sum2 = .
gen sum3 = .

replace secession = secessiona + collapsea
replace sum2 = secession + transfera
replace sum3 = sum2 + absorbb

/* NOT USED

cd $OUTPUTDIR
twoway bar sum3 year, bcolor(lime) ///
|| bar sum2 year, bcolor(orange) ///
|| bar secession year, bcolor(red) ///
, xlab(360 "1800" 370 "1850" 380 "1900" 390 "1950" 400 "2000") xtick(360(2)404)  ///
legend(order(1 "Absorption" 2 "Transfer" 3 "Secession")) ///
ytitle(# events) xtitle("") graphregion(color(white)) bgcolor(white)
graph export BorderChange_Europe.png, width(1275) replace
graph export BorderChange_Europe.pdf, replace
*/

// OUTPUT TABLE A8: ALL EVENTS
twoway bar sum3 year, bcolor(midgreen) ///
|| bar sum2 year, bcolor(gold) ///
|| bar secession year, bcolor(cranberry) ///
, xlab(360 "1800" 370 "1850" 380 "1900" 390 "1950" 400 "2000") xtick(360(2)404)  ///
legend(order(1 "Absorption" 2 "Transfer" 3 "Secession")) ///
ytitle(# events) xtitle("") graphregion(color(white)) bgcolor(white)
graph export BorderChange_Europe.png, width(1275) replace
graph export BorderChange_Europe.pdf, replace

 
 
replace secession = ethseca + ethcollapsea
replace sum2 = secession + irra
replace sum3 = sum2 + ethabsorbb

/* NOT USED
cd $OUTPUTDIR
twoway bar bc year, bcolor(ltblue)  ///
|| bar sum3 year, bcolor(lime) ///
|| bar sum2 year, bcolor(orange) ///
|| bar secession year, bcolor(cherry) ///
, xlab(360 "1800" 370 "1850" 380 "1900" 390 "1950" 400 "2000") xtick(360(2)404)  ///
legend(order(1 "Non-ethnic events" 2 "Unification" 3 "Irredentism" 4 "Secession")) ///
ytitle(# events) xtitle("") graphregion(color(white)) bgcolor(white)
graph export EthBorderChange_Europe.png, width(1275) replace
graph export EthBorderChange_Europe.pdf, replace
*/


// OUTPUT FIGURE 7: ETHNIC AND NON-ETHNIC EVENTS
cd $OUTPUTDIR
twoway bar bc year, bcolor(ltbluishgray)  ///
|| bar sum3 year, bcolor(midgreen) ///
|| bar sum2 year, bcolor(gold) ///
|| bar secession year, bcolor(cranberry)  ///
, xlab(360 "1800" 370 "1850" 380 "1900" 390 "1950" 400 "2000") xtick(360(2)404)  ///
legend(order(1 "Non-ethnic events" 2 "Unification" 3 "Irredentism" 4 "Secession")) ///
ytitle(# events) xtitle("") graphregion(color(white)) bgcolor(white)
graph export EthBorderChange_Europe.png, width(1275) replace
graph export EthBorderChange_Europe.pdf, replace


  


/*

xxx


tsline ef agfrac, xla(1880(20)2020) yla(0(0.1)0.4) graphregion(color(white)) scheme(s2mono) ///
legend(order(1 "Ethnic Frac." 2 "Territorial Frac. kin AG")) 

tsline efaw agfracaw, xla(1880(20)2020) yla(0(0.1)0.6) graphregion(color(white)) scheme(s2mono) ///
legend(order(1 "Ethnic Frac." 2 "Terr. Frac. Kin Group")) 

tsline efpw agfracpw, xla(1880(20)2020) yla(0(0.1)0.6) graphregion(color(white)) scheme(s2mono) ///
legend(order(1 "Ethnic Frac." 2 "Terr. Frac. Kin Group")) 

tsline efaw agfracaw, xla(1880(20)2020) yla(0(0.1)0.6) graphregion(color(white)) scheme(s2mono) ///
legend(order(1 "Ethnic Frac." 2 "Terr. Frac. Kin Group")) 



tsline efpw ef1 ef2, $G ///
legend(order(1 "All states" 2 "Old states" 3 "New states")) ytitle(Terr. Fractionalization) scheme(s2mono)

tsline efpw efpw1 efpw2, $G ///
legend(order(1 "All states" 2 "Old states" 3 "New states")) ytitle(Terr. Fractionalization) scheme(s2mono)

tsline agfracpw agfracpw1 agfracpw2, $G ///
legend(order(1 "All states" 2 "Old states" 3 "New states")) ytitle(Terr. Fractionalization) scheme(s2mono)


xxx
 tsline lntotarea lntotetharea lntotnetharea
 tsline totarea2 etharea2 netharea2, yla(0(1000000)3000000) xla(1880(20)2020)  ///
legend(order(1 "Actual area" 2 "Ethnic changes" 3 "Non-ethnic changes")) graphregion(color(white)) bgcolor(white) scheme(s2mono) 
 
 xxx
 
 // Export data for combined area graph
 cd "/Users/larsc/polybox/Shared/NASTACdb/StateSizeProject/OutputCShapesagg"
 rename area cshp_area
 save cshp_system.dta, replace
 
 
 
 tsline area, yla(0(500000)2500000) xla(1880(20)2020) ylabel(, angle(0)) ytitle(Mean State Size) ///
 graphregion(color(white)) bgcolor(white) scheme(s2mono)

 
tsline dn, xla(1880(20)2020) graphregion(color(white)) bgcolor(white) scheme(s2mono) 

tsline ethdn, xla(1880(20)2020) graphregion(color(white)) bgcolor(white) scheme(s2mono) 

tsline nethdn, xla(1880(20)2020) graphregion(color(white)) bgcolor(white) scheme(s2mono) 

tsline totarea etharea netharea, yla(0(1000000)3000000) xla(1880(20)2020) ylabel(, angle(0)) ytitle(Mean State Size)  ///
legend(order(1 "Actual state size" 2 "Ethnic trend" 3 "Non-ethnic trend")) graphregion(color(white)) bgcolor(white) scheme(s2mono) 
 

 
 
tsline area, yla(0(500000)2500000) xla(1880(20)2020) ylabel(, angle(0)) ytitle(Mean State Size) ///
graphregion(color(white)) bgcolor(white) scheme(s2mono)

tsline n, xla(1880(20)2020) yla(0(50)200) ytitle(Number of States) graphregion(color(white)) bgcolor(white) scheme(s2mono)


twoway (line area year, yaxis(1)) (line efaw year, yaxis(2)) (line agfracaw year, yaxis(2)), /// 
graphregion(color(white)) bgcolor(white) scheme(s2mono) xla(1880(20)2020) ///
legend (order(1 "Terr. State Size" 2  "Ethnic Frac." 3 "Terr. Frac. Kin Group")) ///
ytitle(Mean State Size)




// color
twoway (line area year, yaxis(1) lwidth(thick)) (line efaw year, yaxis(2) lcolor(red) lwidth(thick)) (line agfracaw year, yaxis(2) lcolor(green) lwidth(thick)), /// 
graphregion(color(white)) bgcolor(white) xla(1880(20)2020) ///
legend (order(1 "Terr. State Size" 2  "Ethnic Frac." 3 "Terr. Frac. Kin Group")) ///
ytitle(Mean State Size) 

// BW

twoway (bar area year, yaxis(1) color(gs13) ylabel(, angle(0))) (line ef year, yaxis(2)  lwidth(medium) lpattern(solid)) (line agfrac year, yaxis(2) lwidth(medium) lpattern(dash)), /// 
graphregion(color(white)) bgcolor(white) scheme(s2mono) xla(1880(20)2020) ///
legend (order(1 "State Size" 2  "Ethnic Frac. Country" 3 "Terr. Frac. Kin Group")) ///
ytitle(Mean State Size)

twoway (bar area year, yaxis(1) color(gs13) ylabel(, angle(0))) (line efaw year, yaxis(2)  lwidth(medium) lpattern(solid)) (line agfracaw year, yaxis(2) lwidth(medium) lpattern(dash)), /// 
graphregion(color(white)) bgcolor(white) scheme(s2mono) xla(1880(20)2020) ///
legend (order(1 "State Size" 2  "Ethnic Frac. Country" 3 "Terr. Frac. Kin Group")) ///
ytitle(Mean State Size)


twoway (bar area year, yaxis(1) color(gs13) ylabel(, angle(0))) (line intdjaw year, yaxis(2)  lwidth(medium) lpattern(solid)) (line extdjaw year, yaxis(2) lwidth(medium) lpattern(dash)), /// 
graphregion(color(white)) bgcolor(white) scheme(s2mono) xla(1880(20)2020) ///
legend (order(1 "State Size" 2  "Internal Disjunction" 3 "External Disjunction")) ///
ytitle(Mean State Size)


twoway (bar area year, yaxis(1) color(gs13) ylabel(, angle(0))) (line maxshareaw year, yaxis(2)  lwidth(medium) lpattern(solid)) (line maxagshareaw year, yaxis(2) lwidth(medium) lpattern(dash)), /// 
graphregion(color(white)) bgcolor(white) scheme(s2mono) xla(1880(20)2020) ///
legend (order(1 "Terr. State Size" 2  "Internal share" 3 "External share")) ///
ytitle(Mean State Size)


twoway (bar area year, yaxis(1) color(gs13) ylabel(, angle(0))) (line maxshare year, yaxis(2)  lwidth(medium) lpattern(solid)) (line maxagshare year, yaxis(2) lwidth(medium) lpattern(dash)), /// 
graphregion(color(white)) bgcolor(white) scheme(s2mono) xla(1880(20)2020) ///
legend (order(1 "Terr. State Size" 2  "Dominant share" 3 "Spillover share")) ///
ytitle(Mean State Size)


gen secession = .
gen sum2 = .
gen sum3 = .

replace secession = secessiona + collapsea
replace sum2 = secession + transfera
replace sum3 = sum2 + absorbb
twoway bar sum3 year, bcolor(lime) ///
|| bar sum2 year, bcolor(orange) ///
|| bar secession year, bcolor(red) ///
, xla(1880(20)2020) xla(1880(20)2020)  ///
legend(order(1 "Absorption" 2 "Transfer" 3 "Secession & Collapse")) ///
ytitle(# events) xtitle("") graphregion(color(white)) bgcolor(white)
 
 

replace sum2 = ethseca + irra
replace sum3 = sum2 + ethabsorbb
twoway bar bc year, bcolor(ltblue) ///
|| bar sum3 year, bcolor(lime) ///
|| bar sum2 year, bcolor(orange) ///
|| bar secession year, bcolor(red) ///
, xla(1880(20)2020) xla(1880(20)2020) ///
legend(order(1 "Non-ethnic events" 2 "Unification" 3 "Irredentism" 4 "Secession & Collapse")) ///
ytitle(# events) xtitle("") graphregion(color(white)) bgcolor(white)
  
  
  
  twoway (line intdjaw year, lwidth(medium) lpattern(solid)) (line extdjaw year, lwidth(medium) lpattern(dash)), /// 
graphregion(color(white)) bgcolor(white) scheme(s2mono) xla(1800(50)2020) yla(0(0.1)0.5) ///
legend (order(1  "Internal disjunction" 2 "External disjunction")) ///
ytitle()
// graph export FracGraph_Europe.png, width(1275) replace
// graph export FracGraph_Europe.pdf, replace


  twoway (line intdjaw year, lwidth(thick) lpattern(solid) lcolor(red)) (line extdjaw year, lwidth(thick) lpattern(dash) lcolor(green)), /// 
graphregion(color(white)) bgcolor(white) scheme(s2mono) xla(1800(50)2020) yla(0(0.1)0.7) ///
legend (order(1  "Internal disjunction" 2 "External disjunction")) ///
ytitle()
// graph export FracGraph_Europe.png, width(1275) replace
// graph export FracGraph_Europe.pdf, replace

  twoway (line intdj year, lwidth(thick) lpattern(solid) lcolor(red)) (line extdj year, lwidth(thick) lpattern(dash) lcolor(green)), /// 
graphregion(color(white)) bgcolor(white) scheme(s2mono) xla(1800(50)2020) yla(0(0.1)0.5) ///
legend (order(1  "Internal disjunction" 2 "External disjunction")) ///
ytitle()
// graph export FracGraph_Europe.png, width(1275) replace
// graph export FracGraph_Europe.pdf, replace

merge 

  
  */
